<?php

namespace Home\Controller;

use Think\Controller;

class ProjectController extends Controller {
    function __destruct(){                                                                         // 析构方法  写入用户访问和操作日志
        //require '/home/wwwroot/default/world/behavior.php';
        //require 'behavior.php';
    }

public function isbid_List(){
	
		$showmodel = M ( 'project' ); // 实例化project
		$users = M ( 'users' ); // 实例化users
		$projectcategory = M ( 'projectcategory' );
		$typeId = $_REQUEST ['id'];
		$name=I('post.name');
	 if (I('post.name')!="") {
       $where['name']  = array('like',  '%'.I('post.name').'%');
       $where['leader']  = array('like',  '%'.I('post.name').'%');
       $where['bidinfo']  = array('like', '%'.I('post.name').'%');
       $where['_logic'] = 'or';
       $map['_complex'] = $where;
        $map['ischecked']  = array('eq',1);
       $map['isbid']  = array('eq',1);
		$count=$showmodel->where($map )->order('id desc')->count();
		$p=getpage($count,8);
		$show=$showmodel->where($map )->limit($p->firstRow,$p->listRows)->order('id desc')->select();
		//var_dump($showmodel->getLastSql());
		}
        else{
        	if (strlen ( $typeId ) != 1 || $projectcategory->where ( "id='$typeId'" )->select () == null) {
			$typeId = 'all';
		}
		
		if ($typeId == 'all' || $typeId == null) {
			$number=$showmodel->where('ischecked=1 AND isbid=1')->count();
			$p = getpage($number,8);
			$show=$showmodel->where('ischecked=1 AND isbid=1')->order ( 'isbid asc,modifydate2 desc' )->limit($p->firstRow, $p->listRows)->select ();
			 

		} 
		else {
			$number = $showmodel->where ( "ischecked=1 AND cid = $typeId" )->count (); // 有多少审核通过的项目
			 $p = getpage($number,8);
			$show = $showmodel->where ( "ischecked=1 AND cid = $typeId" )->order ( 'isbid asc,modifydate2 desc' )->limit($p->firstRow, $p->listRows)->select (); // 查询审核通过的项目按发布时间排序
		}
        }
		
	
		for($i = 0; $i < count($show); $i ++) { // 有多少审核通过的项目就循环多少次
			$adddate = $show [$i] ['adddate']; // 取出发布时间
			$sub = substr ( $adddate, 11, 2 ); // 将发布时间的时截取出来
			if ($sub < 12) {
				$show [$i] ['time'] = "上午"; // 判断是否小于12，如果小于12，就是上午，否则就是下午
			   $show [$i] ['num'] = $i; // 计数
				$id = $show [$i] ['id'];
				$intro = $showmodel->where ( "id = $id" )->getField ( 'intro' ); // 取出简介
				$str = strip_tags ( $intro ); // 过滤html标签
				$str = str_replace ( " ", "", "$str" ); // 过滤空格
				$result = mb_substr ( $str, 0, 50, 'utf-8' ); // 截取内容
				$show [$i] ['intro'] = $result;
			} else {
				$show [$i] ['time'] = "下午";
		        $show [$i] ['num'] = $i;
				$id = $show [$i] ['id'];
				$intro = $showmodel->where ( "id = $id" )->getField ( 'intro' );
				$str = strip_tags ( $intro );
				$str = str_replace ( " ", "", "$str" );
				$result = mb_substr ( $str, 0, 50, 'utf-8' );
				$show [$i] ['intro'] = $result;
			}
		}


		
		$this->assign ( 'show', $show );
		$this->assign('page', $p->show()); // 赋值分页输出
	    $this->display();

}
public function isfinish_List(){
        $showmodel = M ( 'project' ); // 实例化project
		$users = M ( 'users' ); // 实例化users
		$projectcategory = M ( 'projectcategory' );
		$typeId = $_REQUEST ['id'];
      if (I('post.name')!="") {
       $where['name']  = array('like',  '%'.I('post.name').'%');
       $where['leader']  = array('like',  '%'.I('post.name').'%');
       $where['bidinfo']  = array('like', '%'.I('post.name').'%');
       $where['_logic'] = 'or';
        $map['_complex'] = $where;
        $map['enddate']  = array('neq','null');
   
	  $count=$showmodel->where($map)->order('id desc')->count();
		$p=getpage($count,8);
		$show=$showmodel->where($map)->limit($p->firstRow,$p->ListRows)->order('id desc')->select();
		}
else{
	if (strlen ( $typeId ) != 1 || $projectcategory->where ( "id='$typeId'" )->select () == null) {
			$typeId = 'all';
		}
		
		if ($typeId == 'all' || $typeId == null) {
			
            $number=$isfinish=$showmodel->where('enddate!="null"')->count();
            $p = getpage($number,10);
			$show=$showmodel->where('enddate!="null"')->order('adddate desc')->limit($p->firstRow, $p->listRows)->select();
			//查询已结项的项目

		} else {
			$number = $showmodel->where ( "ischecked=1 AND cid = $typeId AND enddate!='null'" )->count (); // 有多少审核通过的项目
			$p = getpage($number,10);
			$show = $showmodel->where ( "ischecked=1 AND cid = $typeId AND enddate!='null'" )->order ( 'isbid asc,adddate desc' )->limit($p->firstRow, $p->listRows)->select (); // 查询审核通过的项目按发布时间排序
		}
}
		
		
		for($i = 0; $i < count($show); $i ++) { // 有多少审核通过的项目就循环多少次
			$adddate = $show [$i] ['adddate']; // 取出发布时间
			$sub = substr ( $adddate, 11, 2 ); // 将发布时间的时截取出来
			if ($sub < 12) {
				$show [$i] ['time'] = "上午"; // 判断是否小于12，如果小于12，就是上午，否则就是下午
				//$show [$i] ['num'] = $i; // 计数
				$id = $show [$i] ['id'];
				$intro = $showmodel->where ( "id = $id" )->getField ( 'intro' ); // 取出简介
				$str = strip_tags ( $intro ); // 过滤html标签
				$str = str_replace ( " ", "", "$str" ); // 过滤空格
				$result = mb_substr ( $str, 0, 50, 'utf-8' ); // 截取内容
				$show [$i] ['intro'] = $result;
			} else {
				$show [$i] ['time'] = "下午";
				//$show [$i] ['num'] = $i;
				$id = $show [$i] ['id'];
				$intro = $showmodel->where ( "id = $id" )->getField ( 'intro' );
				$str = strip_tags ( $intro );
				$str = str_replace ( " ", "", "$str" );
				$result = mb_substr ( $str, 0, 50, 'utf-8' );
				$show [$i] ['intro'] = $result;
			}
		}
		

		$this->assign('page', $p->show());
		$this->assign ( 'show', $show );
		
	$this->display();
}
	public function myRelease() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		
		if ($_POST ['haoqingzaitianshuinengyuwozhengfeng'] != null) {
			$project = M ( 'project' );
			$id = $_REQUEST ['id'];
			$data ['enddate'] = date ( 'y-m-d h:i:s', time () );
			if ($_FILES ['attach'] ['name'] != '') {
				$info = $Hint->upload ( 'attach' ); // 调用upload方法
				$attachname = $info ['attach'] ['savepath'] . $info ['attach'] ['savename']; // 存储附件的子目录和名字
				$data ['endattach'] = $attachname;
			}
			$data ['explain'] = $_POST['explain'];
			$data ['isbid'] = 2;
			if ($project->where ( "id = '$id'" )->save ( $data )) {
				$Hint->mysuccee ( "终止项目成功！", "2", "Home/Project/myRelease" );
				die ();
			} else {
				$Hint->myerror ( "失败！", "2" );
				die ();
			}
		} else {
			$number = 1; // 计数器，前台编号累加值
			$myrelease = M ( 'project' ); // 实例化project
			$uid = session ( 'userUid' ); // session取uid
			
			$release = $myrelease->where ( "uid=$uid" )->order ( 'adddate desc' )->select (); // 查询相应的项目
			$this->assign ( 'myrelease', $release );
			$this->assign ( 'number', $number );
		}
		
		$this->display ();
	}
	
	// 查看项目详细
	public function details() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$details = M ( 'project' ); // 实例化project
		$user = M ( 'users' ); // 实例化users
		
		$id = $_REQUEST ['id']; // 取项目的id
		$uid = session ( 'userUid' ); // session取uid
		$role = $user->where ( "uid=$uid" )->getField ( 'role' ); // 取出身份标识位
		                                                          
		// 判断项目是否存在
		if (! ($details->query ( "select * from project where id = '$id' " ))) {
			$Hint->myerror ( "项目不存在!", "2" );
			die ();
		} else {
			// 如果项目存在， 未通过审核，只有管理员和自己可以看到。
		}
		
		if ($role != 3) {
			$project = $details->where ( "id=$id" )->select ();
			$res = $details->where ( "id=$id" )->getField ( 'uid' );
			$users = $user->where ( "uid=$res" )->select ();
			$title = $project [0] ['name']; // 项目标题
			$details->where ( "id=$id" )->setInc ( 'rate', 1 ); // 页面刷新一次数据库hint字段就加1
		} else { // 管理员端入口
			$project = $details->where ( "id=$id" )->select ();
			$res = $details->where ( "id=$id" )->getField ( 'uid' );
			$users = $user->where ( "uid=$res" )->select ();
			$title = $project [0] ['name'];
		}
		
		$this->assign ( 'users', $users );
		$this->assign ( 'title', $title );
		$this->assign ( 'project', $project );
		$this->display ();
	}
	
	// 申报项目
	public function declarePro() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		$users=M('users');
		$number=1;
		$adviser=$users->where('identify= 1')->field('realname')->select();	
	    $this->assign('adviser',$adviser);
	    $this->assign('number',$number);
		if (! empty ( $_POST )) { // 判断表单是否有数据
			$project = M ( 'project' ); // 实例化project
			$projecttype = M ( 'projecttype' ); // 实例化projecttype
			$projectcategory = M ( 'projectcategory' ); // 实例化projectcategory
			$team = M ( 'team' ); // 实例化team

			if (null != $_FILES ['attach'] ['name']) { // 判断文件是否被修改，如果为空则不调用upload方法
				$info = $Hint->upload ( 'attach' ); // 调用upload方法
				$attachname = $info ['attach'] ['savepath'] . $info ['attach'] ['savename']; // 存储附件的子目录和名字
			}
			
			$uid = session ( 'userUid' ); // session取uid
			$isuser = session ( 'userUsername' ); // session取用户名
			$name = $_POST ['name'];
			$leader = $_POST ['leader'];
			$adviser=$_POST['adviser'];
			var_dump($adviser);
			$tid = $_POST ['tid'];
			$cid = $_POST ['cid'];
			var_dump($cid);
			$teamid = $_POST ['teamid'];
			$intro = $_POST ['intro'];
			$schedule = $_POST ['schedule'];
			$memberinfo = $_POST ['memberinfo'];
			$pstate = $_POST ['pstate'];
			if ($_POST ['isbid'] == 1) { // 判断是否招标
				$date ['biddate'] = $_POST ['biddate'];
				$date ['bidinfo'] = $_POST ['bidinfo'];
				$isbid = 1;
			} else {
				$isbid = 2;
			}
			
			$ptype = $projecttype->where ( "id=$tid" )->getField ( 'name' );
			$pcategory = $projectcategory->where ( "id=$cid" )->getField ( 'name' );
			$pteam = $team->where ( "id=$teamid" )->getField ( 'name' );
			
			$date ['uid'] = $uid;
			$date ['isuser'] = $isuser;
			$date ['name'] = $name;
			$date ['leader'] = $leader;
			$date ['adviser'] = $adviser;
			$date ['tid'] = $tid;
			$date ['typename'] = $ptype;
			$date ['cid'] = $cid;
			$date ['categoryname'] = $pcategory;
			$date ['teamid'] = $teamid;
			$date ['teamname'] = $pteam;
			$date ['intro'] = $intro;
			$date ['schedule'] = $schedule;
			$date ['memberinfo'] = $memberinfo;
			$date ['pstate'] = $pstate;
			$date ['isbid'] = $isbid;
			$date ['attach'] = $attachname;
			$date ['adddate'] = date ( "Y-m-d H:i:s" ); // 生成申报日期
			$date ['ischecked'] = 0; // 表示未审核
			
			$result = $project->add ( $date ); // 添加数据
			if ($result) {
				$Hint->mysuccee ( "申报成功，请等待审核！", "2", "Home/Project/myRelease" ); // 成功跳到我发布的项目
				die ();
			} else {
				$Hint->myerror ( "申报失败！", "2" );
				die ();
			}
		} else {
			$this->display ();
		}
	}
	
	// 修改项目
	public function alterProject() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		
		$project = M ( 'project' ); // 实例化project
		$uid = session ( 'userUid' ); // session取uid
		$id = $_REQUEST ['id']; // 取项目的id
		
		if (! empty ( $_POST )) { // 判断表单是否有数据
			$projecttype = M ( 'projecttype' ); // 实例化projecttype
			$projectcategory = M ( 'projectcategory' ); // 实例化projectcategory
			$team = M ( 'team' ); // 实例化team
			
			if (null != $_FILES ['attach'] ['name']) { // 判断文件是否被修改，如果为空则不调用upload方法
				$info = $Hint->upload ( 'attach' ); // 调用upload方法
				$attachname = $info ['attach'] ['savepath'] . $info ['attach'] ['savename']; // 存储附件的子目录和名字
				$date ['attach'] = $attachname;
			}
			
			$tid = $_POST ['tid'];
			$cid = $_POST ['cid'];
			$teamid = $_POST ['teamid'];
			$ptype = $projecttype->where ( "id=$tid" )->getField ( 'name' );
			$pcategory = $projectcategory->where ( "id=$cid" )->getField ( 'name' );
			$pteam = $team->where ( "id=$teamid" )->getField ( 'name' );
			
			$date ['uid'] = $uid;
			$date ['isuser'] = session ( 'userUsername' );
			$date ['name'] = $_POST ['name'];
			$date ['leader'] = $_POST ['leader'];
			$date ['tid'] = $tid;
			$date ['typename'] = $ptype;
			$date ['cid'] = $cid;
			$date ['categoryname'] = $pcategory;
			$date ['teamid'] = $teamid;
			$date ['teamname'] = $pteam;
			$date ['editdate'] = date ( "Y-m-d H:i:s" ); 
			$date ['intro'] = $_POST ['intro'];
			$date ['schedule'] = $_POST ['schedule'];
			$date ['memberinfo'] = $_POST ['memberinfo'];
			$date ['pstate'] = $_POST ['pstate'];
			if ($_POST ['isbid'] == 1) { // 判断是否招标
				$date ['bidinfo'] = $_POST ['bidinfo'];
				$isbid = 1;
			} else {
				$isbid = 2;
			}
			$date ['isbid'] = $isbid;
			
			$result = $project->where ( "uid=$uid and id=$id" )->save ( $date ); // 添加数据
			if ($result) {
				$Hint->mysuccee ( "修改成功！", "2", "Home/Project/myRelease" ); // 成功跳到我发布的项目
				die ();
			} else {
				$Hint->myerror ( "修改失败！", "2" );
				die ();
			}
		} else {
			$release = $project->where ( "uid=$uid and id=$id" )->select (); // 查询相应的项目
			if (! $release) {
				die ( "非法操作!" );
			}
			$this->assign ( 'project', $release );
			$this->display ();
		}
	}
	
	// 删除项目
	public function projectDel() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		
		$project = M ( 'project' ); // 实例化project
		$projectbid = M ( 'projectbid' ); // 实例化projectbid
		$id = $_REQUEST ['id']; // 取出要删除项目的id
		$uid = session ( 'userUid' ); // session取uid
		if (! $project->where ( "id=$id and uid=$uid" )->select ()) {
			die ( "非法访问!" );
		}
		
		$filename = $project->where ( "id=$id" )->getField ( 'attach' ); // 从数据库中取出附件名
		$file = './Public/UPLOADS/attach/' . "$filename"; // 把目录名存到file中
		unlink ( $file ); // 删除真实数据
		
		$result = $projectbid->where ( "pid=$id" )->delete (); // 根据条件删除招聘人员
		$results = $project->where ( "id=$id and uid=$uid" )->delete (); // 根据条件删除项目
		if ($results) {
			echo "0";
			die ();
		} else {
			echo "1";
			die ();
		}
	}
	
	// 加入项目
	public function join() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		
		$project = M ( 'project' ); // 实例化project
		$projectbid = M ( 'projectbid' ); // 实例化projectbid
		$users = M ( 'users' ); // 实例化users
		$uid = session ( 'userUid' ); // session取uid
		$id = I('get.id');

		if (! empty ( $_POST )) { // 判断表单是否有数据
			$username = $users->where ( "uid=$uid" )->getField ( 'username' ); // 取出用户名
			$date ['username'] = $username;
			$date ['uid'] = $uid;
			$date ['pid'] = $_POST ['id'];
			$date ['joindate'] = date ( "Y-m-d H:i:s" ); // 取当前时间
			$date ['isjoined'] = 0;
			$date ['intro'] = $_POST ['intro'];
			
			$result = $projectbid->add ( $date ); // 添加数据
			if ($result) {
				$Hint->mysuccee ( "申请成功，请等待审核！", "2", "Home/Project/myApply" ); // 申请成功跳到项目招标页面
				die ();
			} else {
				$Hint->myerror ( "申请失败！", "2" );
				die ();
			}
		} else {
		if ($uid == null || $id == null) {
			die ( "非法访问!" );
		}
		if ($project->where ( "id='$id'" )->getField ( 'isbid' ) != 1) {
			die ( "此项目不招标!" );
		}
		if ($project->where ( "id='$id'" )->getField ( 'ischecked' ) != 1) {
			die ( "此项目未通过审核!" );
		}
		if ($project->where ( "id='$id'" )->getField ( 'uid' ) == $uid) {
			$Hint->myerror ( "不能申请加入自己的项目！", "1" );
			die ();
		}
		if ($projectbid->where ( "uid='$uid' and pid='$id'" )->select ()) {
			$Hint->myerror ( "已申请过此项目！", "1" );
			die ();
		}
		$number = $projectbid->where ( "uid='$uid' && 	(isjoined = 1 || isjoined = 0)" )->select ();
		if (sizeof ( $number ) >= 2) {
			$Hint->myerror ( "最多只能申请两个项目！", "1" );
			die ();
		}
		$this->assign('id',$id);
		$this->display ();
		}
	}
	
	// 报名名单
	public function applyList() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		
		$number = 1;
		$projectbid = M ( 'projectbid' ); // 实例化projectbid
		$users = M ( 'users' ); // 实例化users
		$project = M ( 'project' ); // 实例化project
		$uid = session ( 'userUid' ); // session取uid
		$id = $_REQUEST ['id'];
		
		if ($project->where ( "id=$id" )->getField ( 'uid' ) != $uid) {
			die ( "非法访问!" );
		}
		
		$apply = $projectbid->where ( "pid=$id && (isjoined=0 || isjoined=1)" )->order ( 'joindate desc' )->select (); // 查询招标的人员
		for($i = 0; $i < sizeof ( $apply ); $i ++) {
			$projectbiduid = $apply [$i] ['uid'];
			$apply [$i] ['realname'] = $users->where ( "uid=$projectbiduid" )->getField ( 'realname' );
			$apply [$i] ['stunum'] = $users->where ( "uid=$projectbiduid" )->getField ( 'stunum' );
			$apply [$i] ['phone'] = $users->where ( "uid=$projectbiduid" )->getField ( 'phone' );
		}	

		$this->assign ( 'apply', $apply );
		$this->assign ( 'number', $number );
		$this->display ();
	}
	
	// 我参与的项目
	public function myJoin() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		
		$project = M ( 'project' ); // 实例化project
		$projectbid = M ( 'projectbid' ); // 实例化projectbid
		$uid = session ( 'userUid' ); // session取uid
		$projectJoin = $projectbid->where ( "uid=$uid AND isjoined=1" )->getField ( 'pid', true );
		$myJoin = array ();
		for($i = 0; $i < sizeof ( $projectJoin ); $i ++) {
			$id = $projectJoin [$i];
			$myJoin [$i] = $project->where ( "id=$id" )->select (); // 查询相应的项目
		}
		$this->assign ( 'myJoin', $myJoin );
		$this->display ();
	}
	
	// 我的申请列表
	public function myApply() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		$projectbid = M ( 'projectbid' );
		$project = M ( 'project' );
		$uid = session ( 'userUid' ); // session取uid
		$projectbidData = $projectbid->where ( "uid = $uid" )->select ();
		for($i = 0; $i < sizeof ( $projectbidData ); $i ++) {
			$pid = $projectbidData [$i] ['pid'];
			$projectbidData [$i] ['name'] = $project->where ( "id =$pid" )->getField ( 'name' );
		}
		$this->assign ( 'info', $projectbidData );
		$this->display ();
	}
	
	// 我的申请信息删除
	public function applyDel() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		$projectbid = M ( 'projectbid' );
		$id = $_REQUEST ['id'];
		$uid = session ( 'userUid' ); // session取uid
		if ($projectbid->where ( "uid = '$uid' && id = '$id'" )->select ()) { // 验证是否越权
			if ($projectbid->where ( "id ='$id'" )->delete ()) { // 删除数据
				echo "0"; // 返回0即成功
			} else {
				echo "1";
			}
		} else {
			$Hint->myerror ( "小伙你要干什么", "2" );
		}
	}
	
	// 中标人员信息导出
	public function export_successful_personnel_information_table(){
	    $Hint = new \Operation\Controller\HintController (); // 实例化Hint
	    $project_id = $_REQUEST['id'];header("Content-type: text/html; charset=utf-8"); 
	    $Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
	    $successful_personnel = $Model->query("select *  from  users left join projectbid on users.uid= projectbid.uid where pid = $project_id && isjoined = 1");
	    if(!empty($successful_personnel)){
	        derived_excel($successful_personnel);
	    }else {
	        $Hint->myerror ( "没有检索到相关数据", "2" );
	    }
	    
	}
	
	
	// 管理项目ajax调用函数
	public function change() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证是否登陆
		$project = M ( 'project' ); // 实例化project
		$projectbid = M ( 'projectbid' ); // 实例化project
		$id = $_REQUEST ['id'];
		$o = $_REQUEST ['o'];
		switch ($o) {
			case 1 :
				$result = $project->where ( "id=$id" )->setField ( 'isbid', 2 );
				if ($result) {
					echo "1";
				}
				break;
			case 2 :
				$result = $project->where ( "id=$id" )->setField ( 'isbid', 1 );
				if ($result) {
					echo "1";
				}
				break;
			case 3 :
				$result = $projectbid->where ( "id=$id" )->setField ( 'isjoined', 1 );
				if ($result) {
					echo "1";
				}
				break;
			case 4 :
				$result = $projectbid->where ( "id=$id" )->setField ( 'isjoined', 2 );
				if ($result) {
					echo "1";
				}
				break;
		}
	}
}